<?php

namespace app\api\controller;

use app\api\model\User as UserModel;
use Yurun\OAuthLogin\Weixin\OAuth2;
use think\Session;
use app\api\model\Wxapp as WxappModel;
use app\api\model\store\shop as Shop;
/**
 * 用户管理
 * Class User
 * @package app\api
 */
class User extends Controller
{
    /**
     * 用户自动登录
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function login()
    {
        $model = new UserModel;
        return $this->renderSuccess([
            'user_id' => $model->tlogin($this->request->post()),
            'token' => $model->getToken()
        ]);
    }

    /**
     * 用户激活账号
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function activation(){
        $user = $this->getUser();
        $model = new UserModel;
        if ($model->activation($this->request->post(),$user->user_id)){
            return $this->renderSuccess([],'激活成功');
        }else{
            return $this->renderError($model->getError() ?: '商品信息不存在');
        }
    }

    /**
     * 当前用户详情
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $model = new UserModel;
        // 当前用户信息
        $userInfo = $this->getUser();
        $store = $model->getStore($userInfo);
        return $this->renderSuccess(compact('userInfo','store'));
    }


    /**
     * 获取登录 并跳转
     */
    public function getqr(){
        $model = new UserModel;
        header('location:' . $model->getqrurl());
        exit();
    }

    /**
     * 扫码后回调
     * 获取用户的openid
     * @throws \Yurun\OAuthLogin\ApiException
     */
    public function checklogin(){
        $wxConfig = WxappModel::getWxappCache();
        $oauth2 = new OAuth2($wxConfig['app_id'],$wxConfig['app_secret']);
        $state = Session::get('YURUN_WX_STATE'.WxappModel::$wxapp_id);
        $code = input('get.code');
        $state_get = input('get.state');

        $accessToken = $oauth2->getAccessToken($state,$code,$state_get);
        $userInfo = $oauth2->getUserInfo();
        $model = new UserModel;
        $user_id = $model->lessonlogin($userInfo);
        $token = $model->getToken();
        $wxapp_id = WxappModel::$wxapp_id;
        $is_activation = $model->hasActivation($user_id)?1:0;
        $is_endtime = $model->isEndtime($user_id);
        $url = "http://".$wxConfig['app_url']."/transfer.html?token=".$token."&wxapp_id=".$wxapp_id."&is_activation=".$is_activation."&is_endtime=".$is_endtime;
        $this->redirect($url);
    }

    /**
     * 获取用户对应的机构内容
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function getstore(){
        $model = new UserModel;
        $userInfo = $this->getUser();
        $data = $model->getStore($userInfo);
        return $this->renderSuccess(compact('data'));
    }
}
